<?php
	include_once("config.php");
?>
<html>
<head>
<title> <? echo XML_TITLE;?> </title>
<p class=titulo align="center"><font face="Arial Black"><? echo XML_TITLE_GERA; ?></font>
<p class=titulo align="center"><font face="Arial Black"><? echo XML_TITLE_SELA; ?></font></p>
<p class=titulo align="center"><font face="Arial Black"><? echo XML_GERA_SELA_SUBTITLE_4; ?></font></p>
<p>
<?
	require_once("verifica.php");
?>
</head>

<body>

<? if (strlen(@$arquivo)==0) { ?>

		<br>
		<p align="left"><font face="Arial"><? echo XML_GERA_MSG01; ?></font>
		<br>
		<form enctype="multipart/form-data" action="index.php?inc=gera_prob_sela_ludecomp_xml.php" method="post">
		<input type="radio" name="arquivo" value="nao" unchecked><font face="Arial"><? echo XML_GERA_MSG02; ?></font><br>
		<input type="radio" name="arquivo" value="sim" unchecked><font face="Arial"><? echo XML_GERA_MSG03; ?></font><p>
		<input class="button" type="submit" name="Submit" value=<? echo XML_NEXT; ?>>
		</form>

<? } else if (($arquivo=="sim")&((strlen(@$text_file)==0)||(!file_exists(@$text_file)))&(@$file_read!="ok")) { ?>

		<br>
		<p align="left"><font face="Arial"><? echo XML_GERA_MSG04; ?></font>
		<br>
		<form enctype=multipart/form-data action="index.php?inc=gera_prob_sela_ludecomp_xml.php" method="post">
		<a href=javascript:winopen('ajuda/ajdados1.php?idioma=PT_BR.php')><font face="Arial"><? echo XML_GERA_MSG05; ?></a><p>
		<input type="file" name="text_file"><p>
		<? if ((strlen($text_file)!=0)&(!file_exists($text_file))) { ?>
              <font face="Arial"><? echo XML_GERA_MSG06; ?></font>
        <? } ?>
		<input type="hidden" name="arquivo" value="<? echo $arquivo ?>">
		<input class="button" type="submit" name="Submit" value=<? echo XML_NEXT; ?>>
		</form>


<? } else if (((strlen(@$dim)==0) or (@$dim<1))& ($arquivo=="nao")) { ?>

		<form enctype=multipart/form-data action="index.php?inc=gera_prob_sela_ludecomp_xml.php" method="post">
		<p align="left"><font face="Arial"><? echo XML_GERA_SELA_MSG01; ?></font>
		<b></b>
		<input type="text" size='2' maxlength='2' name="dim" value="<? echo @$dim ?>">
		<br><br>
		<input type="hidden" name="arquivo" value="<? echo @$arquivo ?>">
		<input class="button" type="submit" name="Submit" value="<? echo XML_NEXT; ?>">
		</form>

<? } else if  (strlen(@$desc)==0 || (!checaNumerico(@$A))) { ?>

		<form name="myform" enctype="multipart/form-data" method="post">

		<script language="javascript">
			function sub(value) {
		  	  if (value == 'a'){
				document.myform.action = 'index.php?inc=ludecomp.php';
				document.myform.target = '_blank';
			  } else {
				document.myform.action = 'index.php?inc=gera_prob_sela_ludecomp_xml.php';
				document.myform.target = '_self';
			  }
		 	  document.myform.submit();
			}
		</script>
		
        <? if (($arquivo=="sim")&(@$file_read!="ok")) {
        $fp = fopen($text_file, "r");
		$vet_dados = file($text_file);
		$dim = trim($vet_dados[0]);
		$cont = 0;
		for ($i=0; $i<$dim; $i++){
		   for ($j=0; $j<=$dim; $j++){
		    $cont = $cont + 1;
			$A[$i][$j]=trim($vet_dados[$cont]);
			   }
		}
		fclose($fp);
		$file_read = ok;
		}
		if (isset($A))
			echo "<font color=red>Os dados de entrada devem ser numericos</font>";
		?>
 
		<p align="left"><font face="Arial"><? echo XML_GERA_SELA_MSG01; ?> <? echo @$dim ?></font>
		<br><br>
		<p align="left"><font face="Arial"><? echo XML_GERA_SELA_MSG02; ?></font>
		<br><br>
		<? for ($i=0; $i<$dim; $i++) {	?>
	   	<?	for ($j=0; $j<$dim; $j++) {
	                $aux=$j+1;  ?>
			    <input type="text" name="A[<? echo $i ?>][<? echo $j ?>]" value="<? echo @$A[$i][$j] ?>" maxlength="10" size="5">
			    <font face="Arial">X<sub><? echo ("$aux") ?></sub></font>
	        	    <?  if ($j<($dim-1)) {
		    			echo (" + ");
				  } //end if
   		      } //end for j
	  		echo (" = ");  ?>
	   		<input type="text" name="A[<? echo $i ?>][<? echo $j ?>]" value="<? echo @$A[$i][$j] ?>" maxlength="10" size="5">
			<br><br>
		<? } //end for i ?>
		<br>
		<p align="left"><font face="Arial"><? echo XML_GERA_SELA_MSG03; ?></font>&nbsp
        <input class="button" type="button" value=<? echo XML_GERA_SELA_MSG04; ?> onClick="sub('a')">
        <input type=hidden name=executar value=sim>
		<br><br>
		<font face="Arial"><? echo XML_GERA_SELA_MSG05; ?></font>
		<? if (@$type_solution=='compatible_determined') { ?>
		<input type="radio" name="type_solution" value="compatible_determined" checked>
		<? } else { ?>
        <input type="radio" name="type_solution" value="compatible_determined" unchecked>
        <? } ?>
        <font face="Arial"><? echo XML_GERA_SELA_MSG06; ?></font>&nbsp&nbsp
		<? if (@$type_solution=='compatible_undetermined') { ?>
		<input type="radio" name="type_solution" value="compatible_undetermined" checked>
		<? } else { ?>
		<input type="radio" name="type_solution" value="compatible_undetermined" unchecked>
        <? } ?>
		<font face="Arial"><? echo XML_GERA_SELA_MSG07; ?></font>&nbsp&nbsp
		<? if (@$type_solution=='unsolvable') { ?>
		<input type="radio" name="type_solution" value="unsolvable" checked>
		<? } else { ?>
		<input type="radio" name="type_solution" value="unsolvable" unchecked>
        <? } ?>
		<font face="Arial"><? echo XML_GERA_SELA_MSG08; ?></font>
        <br><br>
		<table>
			<tr><td>&nbsp</td>
			    <td align="center"><font face="Arial"><? echo XML_GERA_SELA_MSG09; ?></font></td>
			    <td align="center">&nbsp&nbsp<font face="Arial"><? echo XML_GERA_SELA_MSG010; ?></font></td>
			</tr>
			<tr><td><font face="Arial"><? echo XML_GERA_SELA_MSG011; ?></font></td>
                <? if (@$symetric=='symetric') { ?>
			    <td align="center"><input type="radio" name="symetric" value="symetric" checked></td>
		        <? } else { ?>
		        <td align="center"><input type="radio" name="symetric" value="symetric" unchecked></td>
                <? } ?>
                <? if (@$symetric=='nonsymetric') { ?>
			    <td align="center"><input type="radio" name="symetric" value="nonsymetric" checked></td>
			    <? } else { ?>
			    <td align="center"><input type="radio" name="symetric" value="nonsymetric" unchecked></td>
                <? } ?>
			</tr>
			<tr><td><font face="Arial"><? echo XML_GERA_SELA_MSG012; ?></font></td>
                <? if (@$sparsity=='sparse') { ?>
			    <td align="center"><input type="radio" name="sparsity" value="sparse" checked></td>
			    <? } else { ?>
   			    <td align="center"><input type="radio" name="sparsity" value="sparse" unchecked></td>
   			    <? } ?>
                <? if (@$sparsity=='nonsparse') { ?>
			    <td align="center"><input type="radio" name="sparsity" value="nonsparse" checked></td>
			    <? } else { ?>
			    <td align="center"><input type="radio" name="sparsity" value="nonsparse" unchecked></td>
			    <? } ?>
			</tr>
			<tr><td><font face="Arial"><? echo XML_GERA_SELA_MSG013; ?></font></td>
                <? if (@$positive_definite=='positive_definite') { ?>
			    <td align="center"><input type="radio" name="positive_definite" value="positive_definite" checked></td>
			    <? } else { ?>
			    <td align="center"><input type="radio" name="positive_definite" value="positive_definite" unchecked></td>
			    <? } ?>
                <? if (@$positive_definite=='nonpositive_definite') { ?>
			    <td align="center"><input type="radio" name="positive_definite" value="nonpositive_definite" checked></td>
			    <? } else { ?>
			    <td align="center"><input type="radio" name="positive_definite" value="nonpositive_definite" unchecked></td>
			    <? } ?>
			</tr>
			<tr><td><font face="Arial"><? echo XML_GERA_SELA_MSG014; ?></font></td>
                <? if (@$diagonally_dominant=='diagonally_dominant') { ?>
			    <td align="center"><input type="radio" name="diagonally_dominant" value="diagonally_dominant" checked></td>
			    <? } else { ?>
			    <td align="center"><input type="radio" name="diagonally_dominant" value="diagonally_dominant" unchecked></td>
			    <? } ?>
                <? if (@$diagonally_dominant=='nondiagonally_dominant') { ?>
			    <td align="center"><input type="radio" name="diagonally_dominant" value="nondiagonally_dominant" checked></td>
			    <? } else { ?>
			    <td align="center"><input type="radio" name="diagonally_dominant" value="nondiagonally_dominant" unchecked></td>
			    <? } ?>
			</tr>
		</table>
		<br><br>
		<p align="left"><font face="Arial"><? echo XML_GERA_MSG07; ?></font>
		<b></b>
		<input type="text" size='50' maxlength='50' name="desc" value="<? echo @$desc ?>">
		<br><br>
		<p align="left"><font face="Arial"><? echo XML_GERA_MSG08; ?></font><br>
		<TEXTAREA name="com" cols="50" rows="3"><? echo @$com ?></TEXTAREA>
		<p>
		<p align="left"><font face="Arial"><? echo XML_GERA_MSG010; ?></font><br>
		<TEXTAREA name="ref" cols="50" rows="3"><? echo @$ref ?></TEXTAREA>
		<p>		
		<input type="hidden" name="arquivo" value="<? echo @$arquivo ?>">
	    <input type="hidden" name="file_read" value="<? echo @$file_read ?>">
		<input type="hidden" name=dim value="<? echo @$dim ?>">
		<input class="button" type="button" onclick="sub('b')" value="<? echo XML_GERA_MSG010; ?>">
		</form>


<? } else {

$id_demo=0;
//$usr_file = $DOCUMENT_ROOT."/user/".$id_usuario."/biblioteca.xml";
$usr_file=$_SESSION['ANAWEB']->RetornaArquivoBiblioteca();
$dem_file = $DOCUMENT_ROOT."/user/".$id_demo."/biblioteca.xml";

		function xmlentities($xml)
		{
			$entities = array ('<' => '&lt;','&' => '&amp;',);
			if (is_string($xml)) { $xml = strtr($xml, $entities);};
			return $xml;
		}

		//Funcoes DOMXML Versao => 2.4.21
function gera_bib($xml_file,$desc,$dim,$A,$type_solution,$symetric,$sparsity,$positive_definite,$diagonally_dominant,$com,$ref)
{
		if (file_exists($xml_file)) {
            $objeto = new DOMDocument();
            $objeto->preserveWhiteSpace=false;
            $objeto->load($xml_file);
		}
		else {
            $objeto = new DOMDocument('1.0');
		}
        $objeto->formatOutput=true;
        if ($objeto->getelementsbytagname("problems")->length == 0)
        {
            $raiz = $objeto->createElement("problems");
            $raiz = $objeto->appendChild($raiz);
        }
        $raiz = $objeto->getelementsbytagname("problems")->item(0);

		if ($objeto->getelementsbytagname("lin_systems")->length == 0) {
			$filho = $objeto->createElement("lin_systems");
            $filho = $raiz->appendChild($filho);
		}
		$filho = $objeto->getelementsbytagname("lin_systems")->item(0);
        $system = $objeto->createElement("system");
        $system = $filho->appendChild($system);
        $method = $objeto->createElement("method",xmlentities(utf8_encode('direct')));
        $method = $system->appendChild($method);
        $descri = $objeto->createElement("description",xmlentities(utf8_encode($desc)));
        $descri = $system->appendChild($descri);
        $ord = $objeto->createElement("dim",$dim);
		$ord = $system->appendChild($ord);
		for($i = 0; $i < $dim; $i++)
		{ 	$lin = $objeto->createElement("row");
            $lin = $system->appendChild($lin);
			for($j = 0; $j < $dim+1; $j++)
			{ 	$x = $objeto->createElement("x",$A[$i][$j]);
                $x = $lin->appendChild($x);
			}
		}
		if (!strlen($type_solution)==0) {
            $prop = $objeto->createElement("properties");
            $prop->setAttribute("type_solution",$type_solution);
            $prop = $system->appendChild($prop);
		}
		if (!strlen($symetric)==0) {
            $prop = $objeto->createElement("properties");
            $prop->setAttribute("symetric",$symetric);
            $prop = $system->appendChild($prop);
		}
		if (!strlen($sparsity)==0) {
            $prop = $objeto->createElement("properties");
            $prop->setAttribute("sparsity",$sparsity);
            $prop = $system->appendChild($prop);
		}
		if (!strlen($positive_definite)==0) {
            $prop = $objeto->createElement("properties");
            $prop->setAttribute("positive_definite",$positive_definite);
            $prop = $system->appendChild($prop);
		}
		if (!strlen($diagonally_dominant)==0) {
            $prop = $objeto->createElement("properties");
			$prop->setAttribute("diagonally_dominant",$diagonally_dominant);
            $prop = $system->appendChild($prop);
		}
		if (!strlen($com)==0) {
			$comen = $objeto->createElement("comments",xmlentities(utf8_encode($com)));    
			$comen = $system->appendChild($comen);
		}
		if (!strlen($ref)==0) {
			$refer = $objeto->createElement("reference",xmlentities(utf8_encode($ref)));    
			$refer = $system->appendChild($refer);
		}
        $objeto->save($xml_file);
}
gera_bib($usr_file,$desc,$dim,$A,$type_solution,$symetric,$sparsity,$positive_definite,$diagonally_dominant,$com,$ref);
//gera_bib($dem_file,$desc,$dim,$A,$type_solution,$symetric,$sparsity,$positive_definite,$diagonally_dominant,$com,$ref);
        ?>
		<p align="center"><font face="Arial"><? echo XML_GERA_MSG011; ?></font></p>
        <?


} //end else

?>

		<form name="back" method="post" action="index.php?inc=gera_prob_sela_ludecomp_xml.php">
		<input class="button" type="submit" name="Submit" value="<? echo XML_BEGIN; ?>">
		</form>

</body>
</html>
